package com.boco.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.boco.entity.EMR_MJZGHJL;
import com.boco.entity.RS_HZJBXX;

public class GetEMR_MJZGHJLData {
	//按照传来的表名去取得核心库的数据，并返回。
	public static List<EMR_MJZGHJL> getData()
	{
	    Connection con = null;// 创建一个数据库连接
	    PreparedStatement pre = null;// 创建预编译语句对象，一般都是用这个而不用Statement
	    ResultSet result = null;// 创建一个结果集对象
	    List<EMR_MJZGHJL> re = new ArrayList<EMR_MJZGHJL>();
	    try
	    {
	        Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序
	        System.out.println("开始尝试连接数据库！");
	        String url = "jdbc:oracle:" + "thin:@172.20.21.12:1521:orcl";// 127.0.0.1是本机地址，XE是精简版Oracle的默认数据库名
	        String user = "sjzx_hxk";// 用户名,系统默认的账户名
	        String password = "boco#1234";// 你安装时选设置的密码
	        con = DriverManager.getConnection(url, user, password);// 获取连接
	        System.out.println("连接成功！");
	        String sql = "select "
					+"'' as  HZZSYH,"
					+"a.kh as HZJKKH,"
					+"'' as YBKH,"
					+"a.jzlsh as MJZH,"
					+"to_char(a.gthsj,'yyyy-MM-dd HH24:mi:ss') as  GHRQSJTHRQSJ,"
					+"a.gthbz as GTHBZ,"
					+"a.ghlb as GHLB,"
					+"a.jzzt as SFJZBS,"
					+"a.xm as XM,"
					+"'' as SZNL,"
					+"a.xb as XBDM,"
					+"a.yljgdm as JZJGDM,"
					+"'' as JZJGMC,"
					+"a.ksbm as JZKSDM,"
					+"a.ksmc as JZKSMC,"
					+"b.zzysgh as JZYSGH,"
					+"b.zzysxm as JZYSMC,"
					+"'' as YLFYZFFS,"
					+"'' as YLFYZFFSDM,"
					+"a.sfjz as JZBZ,"
					+"to_char(a.ywscsj,'yyyy-MM-dd HH24:mi:ss') as BCRQ,"
					+"a.jzlsh as ZRLSH,"
					+"'' as ORGCODE,"
					+"'' as AREACODE,"
					+"to_char(sysdate,'yyyyMMddHH24miss') as UPLOADTIME,"
					+"'' as SOURCEID "
					 +" from tb_mz_ghmxb a left join tb_mz_jzjl b "
					+" on a.yljgdm = b.yljgdm and a.kh = b.kh and a.klx = b.klx";
	        
//	        System.out.println(sql);
// 预编译语句，“？”代表参数
	          
	        pre = con.prepareStatement(sql);// 实例化预编译语句
	        result = pre.executeQuery();// 执行查询，注意括号中不需要再加参数
	        System.out.println("执行成功");
	        while (result.next()){
	            // 当结果集不为空时
//	            System.out.println(result.getInt("YYDAH"));
	        	EMR_MJZGHJL temp = new EMR_MJZGHJL();
	        	temp.setHZZSYH(result.getString("HZZSYH"));
	        	temp.setHZJKKH(result.getString("HZJKKH"));
	        	temp.setYBKH(result.getString("YBKH"));
	        	temp.setMJZH(result.getString("MJZH"));
	        	temp.setGHRQSJTHRQSJ(result.getString("GHRQSJTHRQSJ"));
	        	temp.setGTHBZ(result.getString("GTHBZ"));
	        	temp.setGHLB(result.getString("GHLB"));
	        	temp.setSFJZBS(result.getString("SFJZBS"));
	        	temp.setXM(result.getString("XM"));
	        	temp.setSZNL(result.getString("SZNL"));
	        	temp.setXBDM(result.getString("XBDM"));
	        	temp.setJZJGDM(result.getString("JZJGDM"));
	        	temp.setJZJGMC(result.getString("JZJGMC"));
	        	temp.setJZKSDM(result.getString("JZKSDM"));
	        	temp.setJZKSMC(result.getString("JZKSMC"));
	        	temp.setJZYSGH(result.getString("JZYSGH"));
	        	temp.setJZYSMC(result.getString("JZYSMC"));
	        	temp.setYLFYZFFS(result.getString("YLFYZFFS"));
	        	temp.setYLFYZFFSDM(result.getString("YLFYZFFSDM"));
	        	temp.setJZBZ(result.getString("JZBZ"));
	        	temp.setBCRQ(result.getString("BCRQ"));
	        	temp.setZRLSH(result.getString("ZRLSH"));
	        	temp.setORGCODE(result.getString("ORGCODE"));
	        	temp.setAREACODE(result.getString("AREACODE"));
	        	temp.setUPLOADTIME(result.getString("UPLOADTIME"));
	        	temp.setSOURCEID(result.getString("SOURCEID"));
	        	re.add(temp);
	        }
	    }
	    catch (Exception e)
	    {
	        e.printStackTrace();
	    }
	    finally
	    {
	        try
	        {
	            // 逐一将上面的几个对象关闭，因为不关闭的话会影响性能、并且占用资源
	            // 注意关闭的顺序，最后使用的最先关闭
	            if (result != null)
	                result.close();
	            if (pre != null)
	                pre.close();
	            if (con != null)
	                con.close();
	            System.out.println("数据库连接已关闭！");
	        }
	        catch (Exception e)
	        {
	            e.printStackTrace();
	        }
	    }
		return re;
	}
}
